define(["jquery","bbcare","bch/app/commSearch/commSearch",
	"as/app/dst/dstDetail.servers","as/app/dst/dstDetail.ui","as/app/dst/dstDetail.event"],
	function($,$bb,$commSearch,$servers,$ui,$event){
	var dstDetail = {};
	var status; // "01":从历史记录里过来；

	/**
	 * 选中月龄
	 * answer为答案
	 */
	var monthSelect = function(i,answer){
		// 初始化
		var w_li = 91;
		var w_show = $("#tabShow").width();
		var w_diff = w_li*$("#tabShow > ul > li").length - w_show;
		var params = {};
		params.status = status;
		$event.init(i,w_diff,w_li,params,answer);
		$event.carousel(i,w_diff,w_li);
	}

	/**
	 * 清空
	 */
	var empty = function(i,className,questions){
		$ui.empty({
        	"dom":$("." + className + " .modal-body"),
        });
        $("."+className).modal("show");
        // 确认重新开始
        $("#emptyY").unbind("click");
        $("#emptyY").click(function(){
        	$("."+className).modal("hide");
        	emptyValue(questions);
        	monthSelect(i);
        });
        // 取消
        $("#emptyN").unbind("click");
        $("#emptyN").click(function(){
        	$("."+className).modal("hide");
        });
	}

	/**
	 * 清空模态框
	 */
	var emptyModal = function(i,questions){
		var className = "emptyModal";
		$bb.modal({
			"id":"dstDetail",
			"className":className,
			"tit":"重新开始",
		},function(){
			empty(i,className,questions);
		});
	}

	/**
	 * 设置月龄
	 */
	var onDefinedMonth = function(){
		var data =$(this).data();
		// 找到对应月份的下标，定位
		var i = $(this).val() - 1;
		if( status && status == "01" ){
			monthSelect(i,data.answer);
		}else{
			// 清空模态框
			emptyModal(i,data.questions);
		}
	}

	/**
	 * 清空
	 */
	var emptyValue = function(questions){
		// 清空样式
		for (var i = 0; i < questions.length; i++) {
			var index = questions[i].id - 1;
			$("#tabShow ul li").eq(index).find(".tab-month.actived").removeClass("actived");
			for (var j = 0; j < questions[i].question.length; j++) {
				$("#tabShow ul li").eq(index).find(".tab-quest-box span").eq(j).removeAttr("data-answer").removeClass("actived-success actived-fail");
			}
		}
		// 清空顶部、底部
		$("#topicTop").html("不确定").removeAttr("data-monthId");
        $("#topicBottom").html("不确定").removeAttr("data-monthId");
	}

	/**
	 * 赋值
	 */
	var setValue = function(answer){
		if( answer ){
			for (var i = 0; i < answer.length; i++) {
				var index = answer[i].monthId - 1;
				$("#tabShow ul li").eq(index).find(".tab-month").addClass("actived");
				for (var j = 0; j < answer[i].question.length; j++) {
					$("#tabShow ul li").eq(index).find(".tab-quest-box span").eq(j).attr("data-answer",answer[i].question[j].result);
					if( answer[i].question[j].result == "1" ){
						$("#tabShow ul li").eq(index).find(".tab-quest-box span").eq(j).addClass("actived-success");
					}else{
						$("#tabShow ul li").eq(index).find(".tab-quest-box span").eq(j).addClass("actived-fail");
					}
				}
			}
		}
	}

	/**
	 * 顶部、底部赋值
	 */
	var setTopAndBottom = function(type,$dom){
		var obj = $dom.data(type);
		obj ? $dom.html(obj.monthName).attr("data-monthId",obj.monthId) : $dom.html("不确定").removeAttr("data-monthId");
	}

	/**
	 * 选择能区
	 */
	var onAfSelect = function(){
		$(this).addClass("active").siblings().removeClass("active");
		var data = $(this).data();
		// 顶部、底部赋值
		setTopAndBottom(data.type,$("#topicTop"));
		setTopAndBottom(data.type,$("#topicBottom"));
		var info = data.info;
		var questions = data.questions;
		// 自定义月龄下拉选项初始化
		$ui.definedMonth({
			"id":"definedMonth",
			"list":questions,
		});
		// tab渲染
		$ui.tab({
			"id":"tabShow",
			"list":questions,
		});
		setValue( data.answer );
		// 获取年龄
		var monthAge = info.ageDay ? parseInt( info.ageDay / 30 ) : 0;
		var monthId = $servers.getMonthIdBymonthAge(questions,monthAge);
		$("#definedMonth").unbind("change");
		$("#definedMonth").data({"questions":questions,"answer":data.answer});
		$("#definedMonth").change(onDefinedMonth);
		// 根据年龄，下拉月龄处赋值
		$("#definedMonth").val(monthId);
		monthSelect(monthId-1,data.answer);
	}

	/**
	 * 查询评估量表问卷成功后的回调函数
	 */
	var ajaxPostionCallback = function(data,info){
		if( info.status && info.status == "01" ){
			var result = JSON.parse(info.result);
			if( result.gather ){
				$("#dstDetailType .item.dstDetail-af-intelligence").data("answer",result.gather.intelligence).addClass("actived");
				$("#dstDetailType .item.dstDetail-af-sociology").data("answer",result.gather.sociology).addClass("actived");
				$("#dstDetailType .item.dstDetail-af-sports").data("answer",result.gather.sports).addClass("actived");
				if( result.top ){
					$("#topicTop").data("intelligence",result.top.intelligence);
					$("#topicTop").data("sociology",result.top.sociology);
					$("#topicTop").data("sports",result.top.sports);
				}
				if( result.bottom ){
					$("#topicBottom").data("intelligence",result.bottom.intelligence);
					$("#topicBottom").data("sociology",result.bottom.sociology);
					$("#topicBottom").data("sports",result.bottom.sports);
				}
			}
		}
		$("#dstDetailType .item").unbind("click");
		$("#dstDetailType .item.dstDetail-af-intelligence").data("questions",data.intelligence);
		$("#dstDetailType .item.dstDetail-af-sociology").data("questions",data.sociology);
		$("#dstDetailType .item.dstDetail-af-sports").data("questions",data.sports);
		$("#dstDetailType .item").click(onAfSelect);
		$("#dstDetailType .item").data("info",info);
		$("#dstDetailType .item").eq(0).click();
	}

	/**
	 * 查询用户信息成功后的回调函数
	 */
	var ajaxUserMassageCallback = function(data){
		$ui.info({
			"dom":$("#dstDetailInfo"),
			"info":data,
		});
		$("#dstDetailLook").unbind("click");
		$("#dstDetailLook").data("info",data);
		$("#dstDetailLook").click($event.onInfoClick);
	}

	dstDetail.init = function(){
		var params = $bb.getPageParams();
		status = params.status;
		// $(".dstDetail-wrap").parents(".assess-wrap").length 判断是从患者中心打开 还是从dst菜单打开
		if( $(".dstDetail-wrap").parents(".assess-wrap").length > 0 ){
			$("#dstDetailInfoWrap").hide();
		}
		$commSearch.servers.ajaxUserMassage(params,ajaxUserMassageCallback);
		// 请求评估问卷
		$.extend(params,{"evaluationType":"DST"});
		$commSearch.servers.ajaxPostion(params,ajaxPostionCallback);
	}

	return dstDetail;
});